package com.ruoyi.trainingManagement.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jakarta.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageHelper;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.trainingManagement.service.ItrainingProgramService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.trainingManagement.domain.trainingProgramCourses;
import com.ruoyi.trainingManagement.service.ItrainingProgramCoursesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 培养计划课程明细Controller
 * 
 * @author chenxl
 * @date 2025-04-16
 */
@RestController
@RequestMapping("/trainingManagement/programPourses")
public class trainingProgramCoursesController extends BaseController
{
    @Autowired
    private ItrainingProgramCoursesService trainingProgramCoursesService;
    @Autowired
    private ItrainingProgramService trainingProgramService;

    /**
     * 查询培养计划课程明细列表
     */
    //@PreAuthorize("@ss.hasPermi('trainingManagement:programPourses:list')")
    @GetMapping("/list")
    public TableDataInfo list(trainingProgramCourses trainingProgramCourses)
    {
        // 检查所有培养方案是否都已审核通过
        boolean allApproved = trainingProgramService.checkAllProgramsApproved();
        if (!allApproved) {
            // 如果存在未审核通过的方案，返回空数据
            return getDataTable(new ArrayList<>());
        }

        // 所有方案都已审核通过，正常返回数据
        startPage();
        List<trainingProgramCourses> list = trainingProgramCoursesService.selecttrainingProgramCoursesList(trainingProgramCourses);
        System.out.println("获取trainingProgramCourseslist数据: " + list);
        return getDataTable(list);
    }
    @PostMapping("/listOfStatusList")
    public TableDataInfo listOfStatusList(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10000") Integer pageSize,
            @RequestBody List<Integer> statusList) {
        System.out.println("获取statusList数据: " + statusList);
        System.out.println("获取pageNum数据: " + pageNum);
        System.out.println("获取pageSize数据: " + pageSize);
        // 检查所有培养方案是否都已审核通过
        boolean allApproved = trainingProgramService.checkAllProgramsApproved();
        if (!allApproved) {
            // 如果存在未审核通过的方案，返回空数据
            return getDataTable(new ArrayList<>());
        }
        PageHelper.startPage(pageNum, pageSize);
        // 所有方案都已审核通过，正常返回数据
        //startPage();
        List<trainingProgramCourses> list = trainingProgramCoursesService.selectByStatusList(statusList);
        return getDataTable(list);
    }

    /**
     * 导出培养计划课程明细列表
     */
    @PreAuthorize("@ss.hasPermi('trainingManagement:programPourses:export')")
    @Log(title = "培养计划课程明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, trainingProgramCourses trainingProgramCourses)
    {
        List<trainingProgramCourses> list = trainingProgramCoursesService.selecttrainingProgramCoursesList(trainingProgramCourses);
        ExcelUtil<trainingProgramCourses> util = new ExcelUtil<trainingProgramCourses>(trainingProgramCourses.class);
        util.exportExcel(response, list, "培养计划课程明细数据");
    }

    /**
     * 获取培养计划课程明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('trainingManagement:programPourses:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(trainingProgramCoursesService.selecttrainingProgramCoursesById(id));
    }

    /**
     * 新增培养计划课程明细
     */
    @PreAuthorize("@ss.hasPermi('trainingManagement:programPourses:add')")
    @Log(title = "培养计划课程明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody trainingProgramCourses trainingProgramCourses)
    {
        // 打印前端传递的训练计划课程明细数据
        System.out.println("获取数据: " + trainingProgramCourses);
        trainingProgramCourses.setId(IdUtils.fastSimpleUUID());
        return toAjax(trainingProgramCoursesService.inserttrainingProgramCourses(trainingProgramCourses));
    }
    /*
     * 批量插入培养计划课程明细
     */
    @PreAuthorize("@ss.hasPermi('trainingManagement:programPourses:batchadd')")
    @Log(title = "培养计划课程明细", businessType = BusinessType.INSERT)
    @PostMapping("/batchAdd")
    public AjaxResult batchAdd(@RequestBody List<trainingProgramCourses> trainingProgramCoursesList)
    {
        return toAjax(trainingProgramCoursesService.batchInsertTrainingProgramCourses(trainingProgramCoursesList));
    }
    /**
     * 修改培养计划课程明细
     */
    @PreAuthorize("@ss.hasPermi('trainingManagement:programPourses:edit')")
    @Log(title = "培养计划课程明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody trainingProgramCourses trainingProgramCourses)
    {
        return toAjax(trainingProgramCoursesService.updatetrainingProgramCourses(trainingProgramCourses));
    }

    /**
     * 删除培养计划课程明细
     */
    @PreAuthorize("@ss.hasPermi('trainingManagement:programPourses:remove')")
    @Log(title = "培养计划课程明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(trainingProgramCoursesService.deletetrainingProgramCoursesByIds(ids));
    }
    /**
     * 根据培养计划id查询培养计划课程明细
     */

    @PreAuthorize("@ss.hasPermi('trainingManagement:programPourses:query')")
    @GetMapping("/selecttrainingProgramCoursesByTrainingProgramId/{trainingProgramId}")
    public AjaxResult selecttrainingProgramCoursesByTrainingProgramId(@PathVariable("trainingProgramId") String trainingProgramId)
    {
         System.out.println("Received request for trainingProgramId: " + trainingProgramId); // 添加日志
         List<trainingProgramCourses> list = trainingProgramCoursesService.selecttrainingProgramCoursesByTrainingProgramId(trainingProgramId);
         System.out.println("List fetched from service: " + list); // 添加日志
         // 创建一个包含 rows 字段的对象来返回数据
         Map<String, Object> result = new HashMap<>();
         result.put("rows", list);
         return success(result);
    }
    /**
     * 根据ID修改状态
     *
     * @param id     课程排课详情ID
     * @param status 要更新的状态值
     * @return 影响行数
     */
    @PutMapping("/updateStatusById/{id}/{status}")
    public int updateStatusById(@PathVariable("id") String id, @PathVariable("status") Integer status) {
        return trainingProgramCoursesService.updateStatusById(id, status);
    }
}
